package com.ttx.pattern.example.chain.tomcat.filterchain;

import com.ttx.pattern.example.chain.tomcat.filterchain.filter.AppendAFilter;
import com.ttx.pattern.example.chain.tomcat.filterchain.filter.ReplaceBFilter;
import com.ttx.pattern.example.chain.tomcat.servlet.ServletRequest;
import com.ttx.pattern.example.chain.tomcat.servlet.ServletResponse;
import com.ttx.pattern.example.chain.tomcat.servlet.SimpleRequest;
import com.ttx.pattern.example.chain.tomcat.servlet.SimpleResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * @author TimFruit
 * @date 19-5-12 上午10:48
 */
public class Client {
    public static void main(String[] args) {
        AppFilterChain filterChain=new AppFilterChain();

        AppFilterConfig appendAFilterConfig=new AppFilterConfig("appendAFilter", AppendAFilter.class.getName());
        filterChain.addAppFilterConfig(appendAFilterConfig); // append "A"


        Map<String,String> BFilterMap=new HashMap<>();
        BFilterMap.put(ReplaceBFilter.REPLACE_STR, "_B_"); //将"B"替换成"_B_"
        AppFilterConfig replaceBFilterConfig=new AppFilterConfig("replaceBFilter", ReplaceBFilter.class.getName(), BFilterMap);
        filterChain.addAppFilterConfig(replaceBFilterConfig);




        ServletRequest request=new SimpleRequest("ABCD");
        ServletResponse response=new SimpleResponse();

        System.out.println("request: "+request.getRequest());
        filterChain.doFilter(request, response);
        System.out.println("after filter, response: "+response.getResponse());
    }
}
